package com.zhj.interview;

import java.util.Arrays;

public class Test11 {
    public static void main(String[] args) {
        String[] ips = {"127.255.255.255","127.168.10.101","127.168.10.102","127.168.10.10","192.168.10.99"};
        mergeSort(ips, 0, ips.length-1, new String[ips.length]);
        System.out.println(Arrays.toString(ips));
        System.out.println(Integer.MAX_VALUE);
    }

    public static void mergeSort(String[] ips, int left , int right, String[] temp) {
        if (left >= right) {
            return;
        }
        int mid = left + (right - left)/2;
        mergeSort(ips, left, mid, temp);
        mergeSort(ips, mid + 1, right, temp);
        merge(ips, left, mid, right, temp);
    }

    /**
     *
     * @param nums
     * @param left 左边索引
     * @param mid 中间索引
     * @param right 右侧索引
     * @param temp 临时数组
     */
    public static void merge(String[] nums, int left, int mid, int right, String[] temp) {
        int i = left;
        int j = mid + 1;
        int tempCurIndex = 0;
        while (i <= mid && j <= right) {
            if (compareIpV4s2(nums[i],nums[j]) <= 0) {
                temp[tempCurIndex] = nums[i];
                tempCurIndex++;
                i++;
            } else {
                temp[tempCurIndex] = nums[j];
                tempCurIndex++;
                j++;
            }
        }

        while (i <= mid) {
            temp[tempCurIndex] = nums[i];
            tempCurIndex++;
            i++;
        }

        while (j <= right) {
            temp[tempCurIndex] = nums[j];
            tempCurIndex++;
            j++;
        }
        tempCurIndex = 0;
        int tempLeft = left;
        while (tempLeft <= right) {
            nums[tempLeft] = temp[tempCurIndex];
            tempCurIndex++;
            tempLeft++;
        }
    }

    /*
     *比较IP大小
     */
    public static int compareIpV4s2(String ip1, String ip2) {
        String[] addrs1 = ip1.split("\\.");
        String[] addrs2 = ip2.split("\\.");
        for (int i = 0; i < 4; i++) {
            int num1 = Integer.parseInt(addrs1[i]);
            int num2 = Integer.parseInt(addrs2[i]);
            if (num1 != num2) {
                return num1 - num2;
            }
        }
        return 0;
    }

    /*
     *比较IP大小
     */
    public static int compareIpV4s(String ip1,String ip2) {
        int result = 0;
        int ipValue1 = getIpV4Value(ip1);     // 获取ip1的32bit值
        int ipValue2 = getIpV4Value(ip2); // 获取ip2的32bit值
        if(ipValue1 > ipValue2)
        {
            result =  -1;
        }
        else if(ipValue1 <= ipValue2)
        {
            result = 1;
        }
        return result;
    }

    public static int getIpV4Value(String ipOrMask) {
        byte[] addr = getIpV4Bytes(ipOrMask);
        // int是4byte，1byte是8位，byte转int时，这时候高位24位会补1，& 0xff可以避免高位补1
        int address1  = addr[3] & 0xFF;
        address1 |= ((addr[2] << 8) & 0xFF00);
        address1 |= ((addr[1] << 16) & 0xFF0000);
        address1 |= ((addr[0] << 24) & 0xFF000000);
        return address1;
    }

    public static byte[] getIpV4Bytes(String ipOrMask) {
        try{
            String[] addrs = ipOrMask.split("\\.");
            int length = addrs.length;
            byte[] addr = new byte[length];
            for (int index = 0; index < length; index++)
            {
                addr[index] = (byte) (Integer.parseInt(addrs[index]));
            }
            return addr;
        }catch (Exception e){
        }
        return new byte[4];
    }
}
